//
//  DAUPerCenterTVC.m
//  DAU-iOS
//
//  Created by XiangDongMBP on 2016/11/18.
//  Copyright © 2016年 East. All rights reserved.
//

#import "DAUPerCenterTVC.h"
#import "DAULoginVC.h"
#import "DAUPatrolVC.h"
#import "JPUSHService.h"

@interface DAUPerCenterTVC ()

@property (strong, nonatomic) IBOutlet UIView *tHeaderBgView;
@property (weak,   nonatomic) IBOutlet UIImageView *tHeaderImgView;
@property (weak,   nonatomic) IBOutlet UIButton *tHeaderNameBtn;
@property (weak,   nonatomic) IBOutlet UIButton *tHeaderArrowBtn;

@end

static NSString * const kReuseCellId = @"UITableViewCell";

@implementation DAUPerCenterTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self initNavigationBar];
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initNavigationBar {
    self.navigationItem.title = NSLocalizedString(@"个人中心", nil);
}

- (void)initSubViews {
    self.tHeaderImgView.layer.cornerRadius = 115 / 2.0;
    self.tHeaderImgView.layer.masksToBounds = YES;
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:DAU_USERDEF_UNAME];
    [self.tHeaderNameBtn setTitle:name forState:UIControlStateNormal];
    [self.tHeaderNameBtn addTarget:self action:@selector(logoutBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.tHeaderArrowBtn addTarget:self action:@selector(logoutBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableHeaderView = self.tHeaderBgView;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuseCellId];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - <UITableViewDelegate>代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DAUPatrolVC *vc = [[DAUPatrolVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - <UITableViewDataSource>代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"person_patrol"];
    cell.textLabel.text = NSLocalizedString(@"巡更", nil);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = DAU_COLOR_FontNor(1.0);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark - 按键处理
- (void)logoutBtnTapped {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:NSLocalizedString(@"您确定要退出该帐号吗？", nil)
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"退出", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self clearLoginData];
        [self jumpToLoginVC];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)jumpToLoginVC {
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *loginVC = [mainSB instantiateInitialViewController];
    [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    //转场动画
    CATransition *anim = [CATransition animation];
    //    anim.type = @"rippleEffect";
    anim.duration = 0.3;
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
}

- (void)clearLoginData {
    // 极光推送，取消用用户的name绑定为别名(退出之后就不用收到推送)
    [JPUSHService setTags:nil aliasInbackground:@""];
    
    // 取消自动登录
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:DAU_USERDEF_AUTOLOGIN];
}

@end
